package org.jample.poi;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jample.poi.bean.FactorData;
import org.jample.poi.util.XlsReader;

public class FactorReader {

    public static FactorData read(String url) {
        HSSFWorkbook wb = XlsReader.read(url);
        HSSFSheet sheet = getFactorSheet(wb);
        return readFactorData(sheet);
    }

    private static FactorData readFactorData(HSSFSheet sheet) {
        FactorData data = new FactorData();
        HSSFRow title = sheet.getRow(1);

        Iterator<HSSFRow> rows = sheet.rowIterator();
        for (int i = 2; i < sheet.getLastRowNum() - 3; i++) {
            HSSFRow row = sheet.getRow(i);
            Map<String, Double> value = new HashMap<String, Double>();
            for (int j = 5; j <= 12; j++) {
                value.put(title.getCell(j).getStringCellValue(), row.getCell(j).getNumericCellValue());
            }
            data.set(row.getCell(1).getStringCellValue(), value);
        }
        return data;
    }

    private static HSSFSheet getFactorSheet(HSSFWorkbook wb) {
        HSSFSheet sheet = wb.getSheetAt(4);
        return sheet;
    }

    public static void main(String[] args) {
        FactorReader reader = new FactorReader();
        FactorData data = reader.read("src/main/resources/poi/factor.xls");
        System.out.println(data);
    }

}
